package br.os.controller;

import java.io.InputStream;
import java.util.Vector;

import br.os.communication.CommAutenticar;
import br.os.communication.CommNotificador;
import br.os.midlet.OSMidlet;
import br.os.model.Message;
import br.os.model.Usuario;
import br.os.util.Util;
import br.os.view.ExitDialog;
import br.os.view.OSAutenticacao;
import br.os.view.OSManager;
import br.os.view.OSMenu;
import br.os.xml.XMLConvert;

import com.sun.lwuit.Command;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;

/**
 * OSController
 * @version 1.0
 */
public class OSController implements ActionListener, CommNotificador {
	private OSMidlet osMidlet;
	private OSMenu osMenu;
	private OSAutenticacao osAutenticacaoView;
	private Usuario usuario;
	private String urlLogin = "http://" + Util.getServerHost() + ":" + Util.getServerPort() + "/OSWeb/Autenticar";
	private Command yesCommand;
	private Command noCommand;
	private ExitDialog exitDialog;

	/**
	 * @param osMidlet
	 */
	public OSController(OSMidlet osMidlet) {
		this.osMidlet = osMidlet;
		setUsuario(new Usuario(false));
		configureExitDialog();
	}

	/**
	 * @return the osAutenticacao
	 */
	public OSAutenticacao getOsAutenticacaoView() {
		return osAutenticacaoView;
	}

	/**
	 * @param osAutenticacao
	 *          the osAutenticacao to set
	 */
	public void setOsAutenticacaoView(OSAutenticacao osAutenticacao) {
		this.osAutenticacaoView = osAutenticacao;
	}

	/**
	 * actionPerformed
	 */
	public void actionPerformed(ActionEvent event) {
		Object source = event.getSource();
		
		if (usuario.isAutenticado()) {
			if (osMenu.getBtSair() == source) {
				exitDialog.show();
			} 
			else if (noCommand == source) {
					exitDialog.dispose();
			} 
			else if (yesCommand == source) {
				this.osMidlet.notifyDestroyed();
			} 
			else if (osMenu.getBtManagerdOS()== source) {
				OSManager osManager = new OSManager(osMenu, usuario.getLogin());
				osManager.show();
			} 
			else if (osMenu.getBtLogoff() == source) {
				opLogoff();
			}else if(osMenu.getBtConfig() == source){
				Dialog.show("OSMobile", "Em Desenvolvimento", "Ok", null);
			}
		} else {
			if (osAutenticacaoView.getCmdSair().equals(source)) {
				this.osMidlet.notifyDestroyed();
			} else if (osAutenticacaoView.getCmdEntrar().equals(source)) {
				opLogin();
			}
		}
	}

	/**
	 * @return the osMidlet
	 */
	public OSMidlet getOsMidlet() {
		return osMidlet;
	}

	/**
	 * @param osMidlet
	 *          the osMidlet to set
	 */
	public void setOsMidlet(OSMidlet osMidlet) {
		this.osMidlet = osMidlet;
	}

	/**
	 * notificarException
	 */
	public void notificarException(Exception exception) {
		Dialog.show("Erro Login", exception.getMessage(), "Ok", null);
	}

	/**
	 * notificarInputStream
	 */
	public void notificarInputStream(InputStream in) {
		Vector vector = XMLConvert.XMLtoObject(in);
		if((vector!=null) && (vector.size()>0)){
		  Message message = (Message) vector.elementAt(0);
		  if(message.getCode().equals("0")){
		  	osMenu = new OSMenu(this);
			osMenu.show();
			usuario.setAutenticado(true);
		  } else {
				Dialog.show("Login", message.getMensagem(), "ok", null);
			}	
		}else{
			Dialog.show("Erro Login", "Problemas na Autenticacao", "ok", null);
		}
	}

	/**
	 * @return the usuario
	 */
	public Usuario getUsuario() {
		return usuario;
	}

	/**
	 * @param usuario
	 *          the usuario to set
	 */
	public void setUsuario(Usuario usuario) {
		this.usuario = usuario;
	}

	/**
	 * opLogin
	 */
	public void opLogin() {
		usuario.setLogin(osAutenticacaoView.gettLogin().getText());
		usuario.setSenha(osAutenticacaoView.gettSenha().getText());
		CommAutenticar comm = new CommAutenticar(urlLogin, usuario, this);
		comm.start();
	}

	/**
	 * opLogoff
	 */
	public void opLogoff() {
		usuario.setAutenticado(false);
		osAutenticacaoView.show();
	}
			
	/**
	 * Configurar Dialog para confirmar saida da aplicação
	 */
	private void configureExitDialog(){
		yesCommand = new Command("Sim", 0);
		noCommand = new Command("Não", 1);
		exitDialog = new ExitDialog();
		exitDialog.addCommand(yesCommand);
		exitDialog.addCommand(noCommand);
		exitDialog.addCommandListener(this); 
	}

}
